﻿using DL.Animation;
using DL.Characters.Player;
using DL.Combat.Base;
using DL.Tools;
using Tools.Unilts;
using UnityEngine;

namespace DL.Combat
{
    public class PlayerBaseWeaponAttackCombatController : CharacterBaseWeaponAttackCombatController
    {
        
        public PlayerBaseWeaponAttackCombatController(Player player) : base(player.CharacterSO.ComboActionConfig)
        {
            Animator = player.Animator;
            Self = player.Self;
            AnimatorEvent = player.AnimationEvent;
            WeaponDetectionCast = player.WeaponDetectionCast;
            WeaponDetectionCast.DetectionCastEvent += TriggerDamaged;
            
            AnimatorEvent.AttackOpenEvent += DamagedDetectionOpen;
            AnimatorEvent.AttackCloseEvent += DamagedDetectionClose;
        }
        
        ~PlayerBaseWeaponAttackCombatController() 
        {
            WeaponDetectionCast.DetectionCastEvent -= TriggerDamaged;
            
            AnimatorEvent.AttackOpenEvent -= DamagedDetectionOpen;
            AnimatorEvent.AttackCloseEvent -= DamagedDetectionClose;
        }

        public override bool ExecuteAttack_Pre()
        {
            if (!CanAttackInput) return false;
            if (Animator.CheckAnimationTag(AnimatorTag.Hit) && Animator.AnimationNormalTime() < .5f) return false;
            if (Animator.CheckAnimationTag(AnimatorTag.Parry) && Animator.AnimationNormalTime() < .5f) return false;
            
            if (Animator.CheckAnimationTag(AnimatorTag.Dead)) return false;
            if (Animator.CheckAnimationTag(AnimatorTag.Dodge)) return false;
            return true;
        }
        
        public override void ActionInput()
        {
            //第一步，配置输入表  
            var comboData = CurrentComboActionConfig.lightComboAction;
            if (comboData == null)
            {
                Debugger.EO_LogWarning("角色未配置轻攻击动作表，请检查！\n 对象为：" + Self.name);
                return;
            }
            //如果没有这个动作表，那么重置到轻攻击
            if (CurrentCombatAction == null)
            {
                ChangeComboAction(comboData);
                Debugger.EO_Log("初始化轻攻击动作");
            }
            else
            {
                var data = CurrentCombatAction.NextCombatAction
                    ? CurrentCombatAction.NextCombatAction
                    : comboData;
                Debugger.EO_Log("准备衔接下一攻击动作：" + data.ComboActionName);
                ChangeComboAction(data);
            }
        }
        
        public override void ExecuteAttackAction()
        {
            Debugger.EO_Log("执行攻击动作：" + CurrentCombatAction.ComboActionName + "\n 攻击间隔时间：" +
                            CurrentCombatAction.ActionColdTime);
            Animator.CrossFade(CurrentCombatAction.ComboActionName,
                CurrentCombatAction.AnimationNormalTime);
            
            //打开武器检测
            // WeaponDetectionCast.AttackCastOpen();
        }
        
        public void EndAttack()
        {
            if (Animator.CheckAnimationTag(AnimatorTag.Motion) && CanAttackInput)
            {
                ChangeComboAction(null);
            }
        }

        /// <summary>
        /// 计算敌人方位的点积(在我身后，则为负数)
        /// </summary>
        /// <returns></returns>
        public float CalculateDirectionEnemyDirection(Transform target)
        {
            if (target == null) return 0f;
            return Vector3.Dot(Self.forward,DevelopmentToos.CalculateDirection(target, Self));
        }
    }
}